# vim:set ft= ts=4 sw=4 et fdm=marker:

use Test::Nginx::Socket::Lua 'no_plan';
use Dotenv -load => 't/.env';

# log_level('warn');

repeat_each(1);


$ENV{TEST_NGINX_TIMESTAMP}        = 1590502289;
$ENV{TEST_NGINX_TIMESTAMP_MS}     = 1590502289.234;

our $http_config = <<'_EOC_';
    lua_package_path "${prefix}../../script/?.ljbc;${prefix}../../script/?.lua;;";

    root "";

    init_by_lua_block {
        local config = require("config")

        -- re-define the current timestamp
        ngx.time = function() return $TEST_NGINX_TIMESTAMP    end;
        ngx.now  = function() return $TEST_NGINX_TIMESTAMP_MS end;
    }
_EOC_

no_shuffle();
no_long_string();

run_tests();

__DATA__


=== TEST REQUIRE /auth
--- http_config eval: $::http_config
--- config
        set $SCRIPT_DIR      "${realpath_root}/../../script";

        location ~ ^/internal/([_\-a-zA-Z0-9/]+)$ {
            internal;
            default_type 'application/json;charset=utf-8';

            content_by_lua_file "$SCRIPT_DIR/api/v1/$1/${request_method}.lua";
        }

        location ~ ^/([_\-a-zA-Z0-9/]+)$ {
            default_type 'application/json;charset=utf-8';

            content_by_lua_block {
                local cjson = require "cjson.safe"
                local ljson = require "shared.test.ljson"

                ngx.req.read_body()
                local res = ngx.location.capture("/internal"..ngx.var.request_uri, {
                    copy_all_vars       = true,
                    always_forward_body = true,
                })
                ngx.status = res.status
                for k, v in pairs(res.header) do
                    ngx.header[k] = v
                end
                ngx.print(ljson.encode(cjson.decode(res.body)))
            }
        }
--- request
REQUIRE /auth
{
    "username"     : "luffy",
    "password"     : "meatmeat",
    "ip"           : "55.11.22.33",
    "ttl"          : 500
}
--- response_body_like chomp
^\{"access_token":"[\-\._A-Za-z0-9]{396}","last_password_change_at":1654528620,"timestamp":1590502289234}$
--- error_code: 200
--- no_error_log
[error]
